package com.qf.android.huihui.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.qf.android.huihui.R;
import com.qf.android.huihui.adapter.show.ShowAdapter;
import com.qf.android.huihui.bean.show.ShowBean;
import com.qf.android.huihui.common.InfoRecyclerView;
import com.qf.android.huihui.view.activity.WebviewActivity;
import com.qf.android.huihui.view.fragment.impl.BaseFragment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 晒物
 */
public class ShowFragment extends BaseFragment // implements Callback<ShowBean>
        implements PullToRefreshBase.OnRefreshListener2 {
    public static final String SHOW_URL = "http://app.huihui.cn/app/shaidan/list?lastpubtime=0&platform=android&system_version=5.1&app_version=3.9.3&device_id=867905022627881&appname=deals_app&vendor=market_360&model=PRO+5&page=";
    private InfoRecyclerView recycler;
    private ShowAdapter adapter;
    private int page = 0;
    private List<ShowBean.DataBean.ListBean> list = new ArrayList<>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_show, container, false);
        init(view);
        return view;
    }

    private void init(View view) {
        recycler = (InfoRecyclerView) view.findViewById(R.id.show_recycler);
        recycler.setMode(PullToRefreshBase.Mode.BOTH);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        recycler.getRefreshableView().setLayoutManager(manager);
        recycler.setOnRefreshListener(this);
        adapter = new ShowAdapter(list, getContext());
        recycler.getRefreshableView().setAdapter(adapter);
        adapter.setItemClickListener(new ShowAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getContext(), WebviewActivity.class);
                intent.putExtra("url",list.get(position).getShaidan().getUrl());
                startActivity(intent);
            }
        });

        //recycler.setLayoutManager(manager);
//        RHttpShow service = new Retrofit.Builder()
//                .baseUrl("http://ww.baidu.com")
//                .addConverterFactory(GsonConverterFactory.create())
//                .build().create(RHttpShow.class);
//        service.getshowData().enqueue(this);

        intiData(0, false, false);


    }

    private void intiData(int pageNum, final boolean isup, final boolean isdown) {
        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        StringRequest request = new StringRequest(StringRequest.Method.GET, SHOW_URL + pageNum, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject object = new JSONObject(response);
                    JSONObject data = object.getJSONObject("data");
                    JSONArray array = data.getJSONArray("list");
                    List<ShowBean.DataBean.ListBean> shaidanBeens = JSON.parseArray(array.toString(), ShowBean.DataBean.ListBean.class);
                    if (isup) {
//                        int count=list.size();
                    } else {
                        list.clear();
                    }
                    list.addAll(shaidanBeens);
                    recycler.onRefreshComplete();
                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, null);
        requestQueue.add(request);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        intiData(0, false, true);

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        intiData(0, true, false);
    }


//
//    @Override
//    public void onResponse(Response<ShowBean> response, Retrofit retrofit) {
////        Log.d("info","-------------"+response.body().hashCode());
//        ShowBean showBean = response.body();
//        ShowDatasBeen data = showBean.getData();
//        list.addAll(data.getList());
//        adapter.notifyDataSetChanged();
////        Log.d("info","----------------走完了"+list.size());
//
//
//    }
//
//    @Override
//    public void onFailure(Throwable t) {
//        t.printStackTrace();
//        Toast.makeText(getContext(), ""+t.getMessage(), Toast.LENGTH_SHORT).show();
//
//    }
}
